package com.netease.xone.fragment;

import preferMgr.XonePrefHelper;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.netease.xone.R;
import com.netease.xone.activity.ActivityMain;
import com.netease.xone.view.CustomViewPager;
import com.netease.xone.view.pagerindicator.CirclePageIndicator;

public class FragmentGuide extends FragmentBase {
    private final String TAG = FragmentGuide.class.getSimpleName();

    private View mRootView;
    private CustomViewPager mViewPager;
    private CirclePageIndicator mIndicator;

    public static FragmentGuide newInstance(Parcelable obj) {
        FragmentGuide f = new FragmentGuide();
        Bundle b = new Bundle();
        b.putParcelable("obj", obj);
        f.setArguments(b);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        if (null == mRootView) {
            mRootView = inflater.inflate(R.layout.fragment_guide, container, false);
        }
        bindView(mRootView);

        return mRootView;
    }

    private void bindView(View view) {
        if (null == view) {
            return;
        }

        mViewPager = (CustomViewPager)view.findViewById(R.id.pager1);
        mViewPager.setAdapter(new GuideAdapter());
        mViewPager.setOffscreenPageLimit(1);
        mViewPager.setOnPageChangeListener(mOnPageChangeListener);

        mIndicator = (CirclePageIndicator)view.findViewById(R.id.indicator);
        mIndicator.setViewPager(mViewPager);
        mIndicator.setOnPageChangeListener(mOnPageChangeListener);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mRootView = null;
        mViewPager = null;
        mIndicator = null;
        mOnPageChangeListener = null;
    }

    private OnPageChangeListener mOnPageChangeListener = new OnPageChangeListener(){

        private int mDeltaX = 0;

        @Override
        public void onPageScrollStateChanged(int arg0) {
            /**
             * 向导最后页滑动进入首页界面
             */
            if (arg0 == ViewPager.SCROLL_STATE_IDLE) {
                if (0 == mDeltaX && mViewPager.getCurrentItem() == (mViewPager.getAdapter().getCount()-1)) {
                    ActivityMain.launch(getActivity());
                    getActivity().finish();
                    XonePrefHelper.putFirstUse(getActivity(), false);
                }
            } else if (arg0 == ViewPager.SCROLL_STATE_DRAGGING) {
                mDeltaX = 0;
            } else {

            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            mDeltaX += arg2;
        }

        @Override
        public void onPageSelected(int arg0) {
        }

    };

    public static class GuideAdapter extends PagerAdapter {
        private int[]  mResourceAarray;
        public GuideAdapter () {
            mResourceAarray = new int[]{
                    R.drawable.icon_app,
                    R.drawable.icon_app,
                    R.drawable.icon_app
            };

        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageview = null;
            imageview = new ImageView(container.getContext());
            if (null != imageview) {
                imageview.setImageResource(mResourceAarray[position]);
                container.addView(imageview, 0);
            }
            return imageview;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View)object);
        }

        @Override
        public int getCount() {
            return mResourceAarray.length;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            if (arg0 == arg1) {
                return true;
            }
            return false;
        }
    }
}
